package com.wzh.project.utils.ssh2;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SSHUtil { 
     
    private ChannelSftp channelSftp; 
     
    private ChannelExec channelExec; 
     
    private Session session = null; 
     
    private int timeout = 60000; 
     
    private static final Logger logger = LogManager.getLogger(SSHUtil.class);

     
	public SSHUtil(SshConfiguration conf) throws Exception {
		// 密码登陆
		logger.info("尝试连接到....host:" + conf.getHost() + ",username:" + conf.getUsername() + ",port:" + conf.getPort());
		JSch jsch = new JSch(); // 创建JSch对象
		session = jsch.getSession(conf.getUsername(), conf.getHost(), conf.getPort()); // 根据用户名，主机ip，端口获取一个Session对象
		session.setPassword(conf.getPassword()); // 设置密码
		Properties config = new Properties();
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config); // 为Session对象设置properties
		session.setTimeout(timeout); // 设置timeout时间
		session.connect(); // 通过Session建立链接
	}
    /**
     * 下载文件
     * @param src 服务器路径
     * @param dst 本地路径
     * @throws Exception
     */
	public void download(String src, String dst) throws Exception {
		channelSftp = (ChannelSftp) session.openChannel("sftp");
		channelSftp.connect();
		channelSftp.get(src, dst);
		channelSftp.quit();
	}
    /**
     * 上传文件
     * @param src  本地路径
     * @param dst  服务器文件路径
     * @throws Exception
     */
	public void upload(String src, String dst) throws Exception {
		channelSftp = (ChannelSftp) session.openChannel("sftp");
		channelSftp.connect();
		channelSftp.put(src, dst);
		channelSftp.quit();
	}
    /**
     * 上传文件
     * @param src  本地路径
     * @param dst  服务器文件路径
     * @throws Exception
     */
	public void upload(InputStream src, String dst) throws Exception {
		channelSftp = (ChannelSftp) session.openChannel("sftp");
		channelSftp.connect();
		channelSftp.put(src, dst);
		channelSftp.quit();
	}
	
	 /**
     * 上传文件
     * @param src  本地路径
     * @param dst  服务器文件路径
     * @throws Exception
     */
	public void upload(InputStream src, String dst,long size,MySftpProgressMonitor monitor) throws Exception {
		channelSftp = (ChannelSftp) session.openChannel("sftp");
		channelSftp.connect();
		channelSftp.put(src, dst, monitor);
		channelSftp.quit();
	}
	
	public String runCmd(String cmd, String charset) throws Exception {
		channelExec = (ChannelExec) session.openChannel("exec");
		channelExec.setCommand(cmd);
		channelExec.setInputStream(null);
		channelExec.setErrStream(System.err);
		channelExec.connect();
		int exeteState = channelExec.getExitStatus();
		logger.debug("====执行的命令是"+cmd+"=========命令行执行结果是： " + exeteState);
		InputStream inerror = channelExec.getErrStream();
		InputStream in = channelExec.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));
		String buf = null;
		StringBuilder sresult = new StringBuilder();
		while ((buf = reader.readLine()) != null) {
			sresult.append(buf + "\n");
		}
		reader.close();
		BufferedReader reader1 = new BufferedReader(new InputStreamReader(inerror, Charset.forName(charset)));
		String buf1 = null;
		StringBuilder sresult1 = new StringBuilder();
		while ((buf1 = reader1.readLine()) != null) {
			sresult1.append(buf1 + "\n");
		}
		reader1.close();
		channelExec.disconnect();
		sresult.append(sresult1);
		logger.debug("===============执行命令行的打印信息为：" + sresult);
		return sresult.toString();
	}
    
	public void close() {
		session.disconnect();
	}
} 